<template>
  <transition name="fade">
    <div v-if="visible" class="image-preview" @click="close" tabindex="0">
      <div class="image-container" @wheel.prevent="handleWheel">
        <img
          :src="imageUrl"
          alt="Preview"
          @click.stop
          :style="{
            transform: `scale(${scale}) rotate(${rotation}deg)`,
            cursor: 'pointer',
          }"
          @mousedown="startDrag"
          @mousemove="onDrag"
          @mouseup="stopDrag"
          @mouseleave="stopDrag"
        />
      </div>
      <div class="controls" @click.stop>
        <div class="el-icon-zoom-in" @click.stop="zoomIn" title="放大"></div>
        <div class="el-icon-zoom-out" @click.stop="zoomOut" title="缩小"></div>
        <div class="el-icon-refresh-left" @click.stop="rotateLeft" title="向左旋转"></div>
        <div class="el-icon-refresh-right" @click.stop="rotateRight" title="向右旋转"></div>
      </div>
      <div class="close-icon" @click.stop>
        <div class="el-icon-close" @click.stop="close" title="关闭"></div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      imageUrl: '',
      scale: 1,
      rotation: 0,
      isDragging: false,
      startX: 0,
      startY: 0,
      translateX: 0,
      translateY: 0,
    }
  },
  beforeDestroy() {
    window.removeEventListener('keydown', this.handleKeyDown, true)
  },
  methods: {
    show(url) {
      this.imageUrl = url
      this.visible = true
      this.$nextTick(() => {
        this.$el.focus() // 使元素可以接收键盘事件
        window.addEventListener('keydown', this.handleKeyDown, true)
      })
    },
    handleKeyDown(event) {
      // if (event.key === 'Escape') {
      //   event.stopPropagation()
      //   this.close()
      // }
    },
    close() {
      this.visible = false
      this.reset()
    },
    reset() {
      this.scale = 1
      this.rotation = 0
      this.translateX = 0
      this.translateY = 0
    },
    zoomIn() {
      this.scale *= 1.2
    },
    zoomOut() {
      this.scale /= 1.2
    },
    rotateLeft() {
      this.rotation -= 90
    },
    rotateRight() {
      this.rotation += 90
    },
    handleWheel(event) {
      if (event.deltaY < 0) {
        this.zoomIn()
      } else {
        this.zoomOut()
      }
    },
    startDrag(event) {
      this.isDragging = true
      this.startX = event.clientX - this.translateX
      this.startY = event.clientY - this.translateY
    },
    onDrag(event) {
      if (this.isDragging) {
        this.translateX = event.clientX - this.startX
        this.translateY = event.clientY - this.startY
      }
    },
    stopDrag() {
      this.isDragging = false
    },
  },
  mounted() {
    window.addEventListener('keydown', (e) => {
      if (e.key === 'Escape' && this.visible) {
        this.close()
      }
    })
  },
  beforeDestroy() {
    window.removeEventListener('keydown', this.close)
  },
}
</script>

<style lang="scss" scoped>
.image-preview {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 99999999;
  outline: none;
}

.image-container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.image-preview img {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
  transition: transform 0.3s ease;
}

.controls {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 20px;
  padding: 20px 30px;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 10px;
  cursor: pointer;
  div {
    font-size: 30px;
    color: #cccc;
    &:hover {
      color: white;
    }
  }
}
.close-icon {
  position: absolute;
  right: 20px;
  top: 20px;
  cursor: pointer;
  padding: 10px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 50%;
  div {
    font-size: 30px;
    color: #cccc;
    &:hover {
      color: white;
    }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
